﻿using DevExpress.XtraEditors;
using DevExpress.XtraGrid.Views.Tile;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace Tool_System_Client
{
    public partial class MainForm : DevExpress.XtraEditors.XtraForm
    {
        DataTable itemTable = new DataTable();

        private string m_sItemLoginIn = "用户登陆";
        private string m_sItemLoginOut = "用户退出";
        private string m_sItemToolBorrow = "工具领用";
        private string m_sItemOrder = "工单查询";
        private string m_sItemTool = "工具查询";

        public MainForm()
        {
            InitializeComponent();
        }

        private void MainForm_Load(object sender, EventArgs e)
        {
            this.WindowState = FormWindowState.Maximized;

            DBManager.GetInstance().Init(@"Data Source =127.0.0.1; database = TS_XY_JX_ICC_Data; uid = sa; pwd = sa; Pooling = false; persist security info = True; workstation id = SERVER; packet size = 4096");

            InitData();
            SetupView();

            tileView1.OptionsBehavior.AllowSmoothScrolling = true;
        }

        protected virtual void InitData()
        {
            try
            {
                itemTable.Columns.Add("ItemName", typeof(String));
                itemTable.Columns.Add("Group", typeof(String));

                UpdateDataTable();

                gridControl1.DataSource = itemTable;
            }
            catch { }
        }

        private void UpdateDataTable()
        {
            itemTable.Rows.Clear();

            DataRow dataRow = null;

            if (DataCache.GetInstance().OUserInfo.BLogin)
            {
                dataRow = itemTable.NewRow();
                dataRow["Group"] = "个人中心";
                dataRow["ItemName"] = m_sItemOrder;
                itemTable.Rows.Add(dataRow);

                dataRow = itemTable.NewRow();
                dataRow["Group"] = "个人中心";
                dataRow["ItemName"] = m_sItemLoginOut;
                itemTable.Rows.Add(dataRow);

                // 
                dataRow = itemTable.NewRow();
                dataRow["Group"] = "工具";
                dataRow["ItemName"] = m_sItemToolBorrow;
                itemTable.Rows.Add(dataRow);
            }
            else
            {
                dataRow = itemTable.NewRow();
                dataRow["Group"] = "个人中心";
                dataRow["ItemName"] = m_sItemLoginIn;
                itemTable.Rows.Add(dataRow);
            }

            dataRow = itemTable.NewRow();
            dataRow["Group"] = "工具";
            dataRow["ItemName"] = m_sItemTool;
            itemTable.Rows.Add(dataRow);
        }

        void SetupView()
        {
            try
            {
                // Setup tiles options
                tileView1.BeginUpdate();
                tileView1.OptionsTiles.RowCount = 3;
                tileView1.OptionsTiles.Padding = new Padding(20);
                tileView1.OptionsTiles.ItemPadding = new Padding(10);
                tileView1.OptionsTiles.IndentBetweenItems = 20;
                tileView1.OptionsTiles.ItemSize = new Size(340, 190);
                //tileView1.Appearance.ItemNormal.ForeColor = Color.White;
                //tileView1.Appearance.ItemNormal.BorderColor = Color.Transparent;
                tileView1.OptionsTiles.Orientation = Orientation.Horizontal;
                tileView1.ColumnSet.GroupColumn = tileView1.Columns["Group"];
                tileView1.ColumnSet.GroupColumn.SortOrder = DevExpress.Data.ColumnSortOrder.Descending;

                //Setup tiles template
                //TileViewItemElement backPanel = new TileViewItemElement();
                TileViewItemElement item = new TileViewItemElement();
                TileViewItemElement group = new TileViewItemElement();
                //tileView1.TileTemplate.Add(backPanel);
                tileView1.TileTemplate.Add(item);
                tileView1.TileTemplate.Add(group);
                //
                //backPanel.StretchVertical = true;
                //backPanel.Width = 122;
                //backPanel.TextLocation = new Point(-10, 0);
                //backPanel.Appearance.Normal.BackColor = Color.FromArgb(58, 166, 101);
                //
                item.Column = tileView1.Columns["ItemName"];
                item.TextAlignment = TileItemContentAlignment.MiddleCenter;
                item.Appearance.Normal.Font = new Font("Segoe UI Semilight", 34F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
                //
                group.Column = tileView1.Columns["Group"];
                group.TextVisible = false;
             }
            finally
            {
                tileView1.EndUpdate();
            }
        }

        private void tileView1_ItemClick_1(object sender, TileViewItemClickEventArgs e)
        {
            var sColumn = tileView1.Columns["ItemName"];
            if (sColumn == null)
                return;

            string sItemName = tileView1.GetRowCellDisplayText(e.Item.RowHandle, sColumn);
            if (sItemName == m_sItemLoginIn)
            {
                LoginForm loginForm = new LoginForm();
                if (loginForm.ShowDialog() == DialogResult.OK)
                {
                    // 登陆成功
                    lc_name.Text = DataCache.GetInstance().OUserInfo.SUserName + ",你好!";
                    UpdateDataTable();
                }
            }
            else if (sItemName == m_sItemLoginOut)
            {
                // 
                DataCache.GetInstance().OUserInfo.Clear();
                lc_name.Text = "游客,你好!";
                UpdateDataTable();
            }
            else if (sItemName == m_sItemToolBorrow)
            {
                // 
                Tool_Apply_Form form = new Tool_Apply_Form();
                form.ShowDialog();
            }
            else if (sItemName == m_sItemOrder)
            {
                // 
                OrderList_Form form = new OrderList_Form();
                form.ShowDialog();
            }
            else if (sItemName == m_sItemTool)
            {
                // 
                Tool_List_Form form = new Tool_List_Form();
                form.ShowDialog();
            }

        }

        Color colorPanelReady = Color.FromArgb(58, 166, 101);
        Color colorPanelSold = Color.FromArgb(158, 158, 158);
        Color colorCaptionReady = Color.FromArgb(193, 222, 204);
        Color colorCaptionSold = Color.FromArgb(219, 219, 219);

        private void tileView1_ItemCustomize(object sender, TileViewItemCustomizeEventArgs e)
        {
            //if (e.Item == null || e.Item.Elements.Count == 0)
            //    return;
            //int status = (int)tileView1.GetRowCellValue(e.RowHandle, tileView1.Columns["Status"]);
            //Color panelColor = colorPanelReady;
            //Color captionColor = colorCaptionReady;
            //if (status == 1)
            //{
            //    panelColor = colorPanelSold;
            //    captionColor = colorCaptionSold;
            //    e.Item.Elements[6].Text = "Sold";
            //}
            //e.Item.Elements[0].Appearance.Normal.BackColor = panelColor;
            //e.Item.Elements[2].Appearance.Normal.ForeColor = captionColor;
            //e.Item.Elements[4].Appearance.Normal.ForeColor = captionColor;
        }
    }
}
